home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Loadstar 158
/
158.d81
/
b.magic squares
(
.txt
)
< prev
next >
Wrap
Commodore BASIC
|
2022-08-26
|
9KB
|
287 lines
2 poke56,56:clr:poke53371,0:poke788,49
4 poke53281,0:poke53280,0:print"[147]":ti$="000000":gosub60000
6 dv=peek(186):ifdv<8thendv=8
8 sys57812"magic sq font",dv,0:poke780,0:poke781,0:poke782,56:sys65493
10 fori=960to1023:readj:pokei,j:next
20 data255,252,0,128,4,0,128,4,0,128,4,0,128,4,0,128,4,0,128,4,0,128,4,0
30 data128,4,0,255,252,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
40 data0,0,0,0,0,0,0,0,0,0
50 goto480
54 :
55 :rem *** getkey routine ***
56 :
57 poke214,23:print:print" [158][208]ress [211][208][193][195][197] [158]to continue":poke198,0
58 geta$:ifa$<>" "then58
59 return
62 :
63 :rem *** clear half screen ***
64 :
65 fori=217to242:pokei,peek(i)or128:next
66 poke214,14:print
67 fori=0to8:printbl$:next:printbl$""
68 poke214,14:print
69 return
72 :
73 :rem *** delay loop ***
74 :
75 fort=0to500:next:return
87 :
88 :rem *** box routine ***
89 :
90 poke214,y:print:poke646,cb
100 printtab(x)tp$
110 forii=0to4
120 printtab(x)m0$:printtab(x)m1$
130 next
140 print"[145]"tab(x)bt$
150 return
160 :
170 :rem *** fill routine ***
180 :
190 poke214,y-1:print:poke646,ct
200 fori=0to4:print""
210 forj=0to4
220 printtab(x+1+3*j)d$(i,j);
230 next:next:print""
240 return
250 :
260 :rem *** solve routine ***
270 :
280 k=(fnr(5)+1)
290 fori=0to4:forj=0to4:d(i,j)=0:d$(i,j)="":next:next
300 fori=0to5:m(i)=0:next
310 fori=0to3
320 m=fnr(5)+1:ifm(m)or(m=k)then320
330 n(i)=m:m(m)=1:next
340 fori=0to4
350 forl=0toe(u,i)-1
360 d(i,l)=n(4-e(u,i)+l):next:p=0
370 forl=e(u,i)+1to4
380 d(i,l)=n(p):p=p+1:next
390 next
400 fori=0to4:d(i,e(u,i))=k:next
410 ifu=0thenfori=0to4:forj=0to4:f(i,j)=d(i,j):next:next
420 ifuthenfori=0to4:forj=0to4:d(i,j)=((d(i,j)-1)*5):next:next
430 fori=0to4:forj=0to4:d$(i,j)=right$(str$(d(i,j)),2):next:next
440 return
450 :
460 :rem *** setup ***
470 :
480 poke53281,0:poke53280,0
490 x=rnd(-ti):def fnr(x)=int(rnd(1)*x)
500 tp$="[176][192][192][178][192][192][178][192][192][178][192][192][178][192][192][174]"
510 m0$="[221] [221] [221] [221] [221] [221]"
520 m1$="[171][192][192][219][192][192][219][192][192][219][192][192][219][192][192][179]"
530 bt$="[173][192][192][177][192][192][177][192][192][177][192][192][177][192][192][189]"
535 bl$=" "
540 fori=0to4:reade(0,i):e(1,i)=4-e(0,i):next
550 data3,0,2,4,1
560 v=53248
570 fori=0to4:pokev+39+i,1:poke2040+i,15:next
580 pokev+28,0:pokev+29,31:pokev+23,31
590 fori=0to1:forj=0to4:readsp(i,j*2),sp(i,j*2+1):next:next
610 data114,68,42,84,90,100,138,116,66,132,226,68,42,84,250,100,202,116,18,132
620 fori=1to3:readw$(i):next
630 data" [199][197][206][197][210][193][212][207][210] "," [212][213][212][207][210][201][193][204][160] "," [204][207][193][196][211][212][193][210] "
638 ifti$<"000003"then638
640 :
650 :rem *** menu ***
660 :
670 print"[147]":poke53272,31
680 print" [159][212]he [205]ighty [156][204][207][193][196][211][212][193][210]"
690 print" [159]5x5 [205]agic [211]quare [199]enerator"
700 print" [155][193]lgorithm by [202]im [205]oran"
705 print" [208]rogram by [203]nees [195]alhoon"
710 poke214,22:print:print" [154][213]se [195][210][211][210] [213][208][154]/[196][207][215][206] [154]and [210][197][212][213][210][206]"
720 x=12:y=9:cb=7:gosub90
730 poke214,10
740 fori=0to9:printtab(13)" ":next
750 poke214,12:print
760 fori=1to3:printtab(13)"[153]"w$(i):print:next:z=1
770 poke214,10+2*z:print:printtab(13)""w$(z)"[145]":poke198,0
780 geta$:ifa$<>"[145]"anda$<>""anda$<>chr$(13)then780
790 ifa$=chr$(13)then840
800 printtab(13)"[153]"w$(z)
810 ifa$="[145]"thenz=z-1:ifz<1thenz=3
820 ifa$=""thenz=z+1:ifz>3thenz=1
830 goto770
840 onzgoto880,2030,1260
850 :
860 :rem *** draw screen ***
870 :
880 print"[147][150] [205]agic [211]quare [199]enerator"
890 x=2:y=1:cb=12:gosub90:printtab(9)"[154][193]"
900 x=22:gosub90:printtab(30)"[154][194]"
910 x=12:y=12:cb=7:gosub90:printtab(14)"[154][205][193][199][201][195] [211][209][213][193][210][197]"
920 poke214,14:print
930 print" [176][192][192][192][192][192][174]"tab(31)"[176][192][192][192][192][192][192][174]"
940 print" [221][155][211][208][193][195][197][221]"tab(31)"[221][155][210][197][212][213][210][206][221]"
950 print" [173][192][192][192][192][192][189]"tab(31)"[173][192][192][192][192][192][192][189]"
960 print"[153] [193]nother"tab(33)"[205]enu"
970 print" [211]quare"
980 :
990 :rem *** fill boxes ***
1000 :
1010 fori=0to4:forj=0to4:f(i,j)=0:next:next
1020 u=0:gosub280
1030 x=2:y=1:ct=3:gosub190
1040 u=1:gosub280:x=22:gosub190
1050 fori=0to4:forj=0to4:d(i,j)=d(i,j)+f(i,j):next:next
1060 gosub430:y=12:x=12:ct=15:gosub190:pokev+21,0
1070 poke198,0
1080 geta$:ifa$<>" "anda$<>chr$(13)then1080
1090 ifa$=" "thengosub90:goto890
1120 goto670
1230 :
1240 :rem *** loadstar ***
1250 :
1260 printtab(13)" [209]uit? (y/n) [145]"
1270 geta$:ifa$<>"y"anda$<>"n"then1270
1280 ifa$="n"then770
1290 print"[147]"
1300 goto40000
2000 :
2010 :rem *** tutorial ***
2020 :
2030 print"[147][129] [200]ow [212]o [205]ake a 5 x 5 [205]agic [211]quare"
2040 print"[155] [198]irst of all, let's define '[205]agic"
2050 print"[211]quare'. [201]t's a square matrix of"
2060 print"numbers whose columns, rows and"
2070 print"diagonals all add up to the same number."
2080 print"[145][212]his number is always the same for any"
2090 print"particular order of magic square. [212]he"
2100 print"formula for finding the number is"
2110 print"[159] (n * (n*n+1))
2120 [153]" number = tantantantantantantantantantantantantan
2130 print" 2
2140 [153]"list(NULL)he number (or 'magic constant') of a"
2150 [153]"5 x 5 square is therefore 65.
2160 print" [212]he following tutorial explains one"
2170 print"method (or algorithm) for generating up"
2180 print"to 14,400 different 5 x 5 magic squares."
2190 gosub57
2200 print"[147][155] [212]his algorithm is called the [211][213][208][197][210]-"
2210 print"[199][197][207][205][197][212][210][201][195] [196][197][211][201][199][206] method and is taken"
2220 print"from [202]im [205]oran's '[212]he [215]onders of [205]agic"
2230 print"[211]quares' book. [212]he method was devised"
2240 print"by [202]im, himself."
2250 print" [201]t involves creating two non-magical"
2260 print"[145]squares and adding them together to"
2270 print"make a third square that [201][211] magical."
2280 print" [194]oth of the preliminary squares"
2290 print"are made the same way. [212]he difference"
2300 print"is that square [193] will contain the"
2310 print"numbers 1, 2, 3, 4 and 5, and [211]quare [194]"
2320 print"uses the numbers 0, 5, 10, 15 and 20."
2340 print" [204]et's look at [211]quare [193]."
2350 gosub57
2360 print"[147]":
2370 x=2:y=1:cb=12:gosub90:printtab(9)"[154][193]"
2380 fori=0to4:pokev+i*2,sp(0,i*2):pokev+1+i*2,sp(0,1+i*2):next
2385 pokev+16,0:pokev+21,31
2390 poke214,15:print
2400 print"[155] [212]he boxed squares represent [202]im's"
2410 print"[211][213][208][197][210]-[199][197][207][205][197][212][210][201][195] [196][197][211][201][199][206]. [210]emember this"
2420 print"design - it's magical.
2430 [153]" (NULL)tart by choosing any number from"
2440 [153]"1 to 5. left$ow about 4? (NULL)ut it in the"
2450 [153]"boxed squares."
2460 [153]"open"[163]13)"4"
2462 [153]""[163]4)"4"
2464 [153]""[163]10)"4"
2466 [153]""[163]16)"4"
2468 [153]""[163]7)"4"
2470 [141]57:[141]65
2480 [153]"list (NULL)ow, using the four other numbers,"
2490 [153]"1, 2, 3 and 5, arrange them in any"
2500 [153]"order. left$ow about 5-2-1-3?
2510 print" [211]tarting with the square to the"
2520 print"right of a box, place these four"
2530 print"numbers [201][206] [211][197][209][213][197][206][195][197], left to right. [201]f"
2540 print"you hit the end of the matrix, 'wrap"
2545 print"around' to the same row.":gosub75
2550 print"[159]"tab(4)"2"tab(7)"1"tab(10)"3"tab(13)""tab(16)"5":gosub75
2560 print""tab(4)""tab(7)"5"tab(10)"2"tab(13)"1"tab(16)"3":gosub75
2570 print""tab(4)"1"tab(7)"3"tab(10)""tab(13)"5"tab(16)"2":gosub75
2580 print""tab(4)"5"tab(7)"2"tab(10)"1"tab(13)"3"tab(16)"":g